package com.ruoyi.web.controller.jiaofu;

import cn.hutool.core.util.URLUtil;
import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jiaofu.domain.DdtJiaofuTopicTopic;
import com.ruoyi.jiaofu.service.DdtJiaofuTopicService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 专题书单Controller
 *
 * @author ruoyi
 * @date 2024-05-26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/jiaofu/topic")
public class DdtJiaofuTopicController extends BaseController {
    private final DdtJiaofuTopicService ddtJiaofuTopicService;

    /**
     * 查询专题书单列表
     */
    @PreAuthorize("@ss.hasPermi('system:topic:list')")
    @GetMapping("/list")
    public TableDataInfo list(DdtJiaofuTopicTopic ddtJiaofuTopic) {
        startPage();
        List<DdtJiaofuTopicTopic> list = ddtJiaofuTopicService.selectDdtJiaofuTopicList(ddtJiaofuTopic);
        return getDataTable(list);
    }

    /**
     * 导出专题书单列表
     */
    @PreAuthorize("@ss.hasPermi('system:topic:export')")
    @Log(title = "专题书单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DdtJiaofuTopicTopic ddtJiaofuTopic) throws IOException {
        List<DdtJiaofuTopicTopic> list = ddtJiaofuTopicService.selectDdtJiaofuTopicList(ddtJiaofuTopic);

        // Step.3 调用AutoPoi导出Excel
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String sheetName = "专题书单数据";
        String fileName = URLUtil.encode(sheetName).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), DdtJiaofuTopicTopic.class).sheet(sheetName).doWrite(list);
    }

    /**
     * 获取专题书单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:topic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ddtJiaofuTopicService.selectDdtJiaofuTopicById(id));
    }
}
